package com.dc.business.datacollectbusiness.mapper;

import com.dc.business.datacollectbusiness.model.HJ212Model;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HJ212ReceiverMapper {

    // 收集HJ212报文信息
    @Insert("INSERT INTO hj212_detect (mn, total_power, voltage_a, voltage_b, voltage_c, " +
            "current_a, current_b, current_c, total_power_consumption, port, detect_time) " +
            "VALUES (#{mn}, #{totalPower}, #{voltageA}, #{voltageB}, #{voltageC}, " +
            "#{currentA}, #{currentB}, #{currentC}, #{totalPowerConsumption}, #{port}, #{detectTime})")
    public void insertHJ212Msg(HJ212Model hj212Model);


    // 拿取34个设备最新的数据信息
    @Select("SELECT * FROM hj212_detect WHERE (mn, detect_time) IN " +
            "(SELECT mn, MAX(detect_time) FROM hj212_detect GROUP BY mn)")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "mn", column = "mn"),
            @Result(property = "totalPower", column = "total_power"),
            @Result(property = "voltageA", column = "voltage_a"),
            @Result(property = "voltageB", column = "voltage_b"),
            @Result(property = "voltageC", column = "voltage_c"),
            @Result(property = "currentA", column = "current_a"),
            @Result(property = "currentB", column = "current_b"),
            @Result(property = "currentC", column = "current_c"),
            @Result(property = "totalPowerConsumption", column = "total_power_consumption"),
            @Result(property = "port", column = "port"),
            @Result(property = "detectTime", column = "detect_time")
    })
    public List<HJ212Model> fetchAllDevicesLatestInfo();

    // 拿取制定MN设备最新的几条数据信息(不输入开始结束时间)
    @Select("SELECT * FROM hj212_detect WHERE mn = #{mn} AND DATE(detect_time) LIKE CONCAT(#{formattedDate}, '%')")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "mn", column = "mn"),
            @Result(property = "totalPower", column = "total_power"),
            @Result(property = "voltageA", column = "voltage_a"),
            @Result(property = "voltageB", column = "voltage_b"),
            @Result(property = "voltageC", column = "voltage_c"),
            @Result(property = "currentA", column = "current_a"),
            @Result(property = "currentB", column = "current_b"),
            @Result(property = "currentC", column = "current_c"),
            @Result(property = "totalPowerConsumption", column = "total_power_consumption"),
            @Result(property = "port", column = "port"),
            @Result(property = "detectTime", column = "detect_time")
    })
    List<HJ212Model> findPageDeviceByMn(@Param("mn") String mn, @Param("formattedDate") String formattedDate);

    // 拿取制定MN设备最新的几条数据信息(输入开始结束时间)
    @Select("SELECT * FROM hj212_detect " +
            "WHERE mn = #{mn} " +
            "AND detect_time BETWEEN #{startTime} AND #{endTime}" +
            "order by detect_time desc")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "mn", column = "mn"),
            @Result(property = "totalPower", column = "total_power"),
            @Result(property = "voltageA", column = "voltage_a"),
            @Result(property = "voltageB", column = "voltage_b"),
            @Result(property = "voltageC", column = "voltage_c"),
            @Result(property = "currentA", column = "current_a"),
            @Result(property = "currentB", column = "current_b"),
            @Result(property = "currentC", column = "current_c"),
            @Result(property = "totalPowerConsumption", column = "total_power_consumption"),
            @Result(property = "port", column = "port"),
            @Result(property = "detectTime", column = "detect_time")
    })
    List<HJ212Model> findPageDeviceByMnAndTime(@Param("mn") String mn, @Param("startTime") String startTime, @Param("endTime") String endTime);


    @Select("SELECT detect_time " +
            "FROM hj212_detect " +
            "WHERE mn = #{mn} " +
            "ORDER BY detect_time DESC " +
            "LIMIT 1")
    String findHJ212ByMn(@Param("mn") String mn);
}
